package com.vce.web.util.jackson;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;

/**
 * Created by IntelliJ IDEA.
 * User: lruiz
 * Date: 8/22/11
 * Time: 10:02 AM
 * To change this template use File | Settings | File Templates.
 */
public class DateDeserializer extends JsonDeserializer<Date> {

    @Override
    public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = null;
        try {
            if (!jp.getText().isEmpty()){
                d = formatter.parse(jp.getText());
            }
        } catch (Exception e) {
            throw new JsonParseException(e.getMessage(),jp.getCurrentLocation(),e);
        }
        return d;
    }
}
